/**
 * 
 * @param {*} sucess 数据库连接成功的回调
 * @param {*} error 数据库连接失败的回调
 */

module.exports = function(sucess,error){
    if(typeof error !== 'function' ){
        error = () => {
            console.log('连接错误');
        }
    }
    //1.安装mongoose
    //2.导入
    const mongoose = require('mongoose');
    //设置strictQuery 为 true
    const {DBHOST,DBPORT,DBNAME} = require('./config/config');
    mongoose.set('strictQuery', true);

    //3.连接 mongodb 服务
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`);

    //4. 设置回调
    mongoose.connection.once('open',()=>{//用once代替on更好
        sucess();
    });//设置连接成功的回调

    mongoose.connection.on('error',()=>{
       error();
    });//设置连接失败的回调
    mongoose.connection.on('close',()=>{
        console.log('连接关闭');
    });//设置连接关闭的回调
}